<?php

namespace app\money\controller;

use app\admin\common\Base;
use app\admin\common\Excel;
use think\Db;
// 客户对账单
class Customersreconciliation extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if ($map == '') {
                $list = Db::name('order')->where('big_type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->select();
                $count = Db::name('order')->where('big_type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->count();
            } else {
                $list = Db::name('order')->where('big_type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->select();
                $count = Db::name('order')->where('big_type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->limit($start, $limit)->count();
            }

            foreach ($list as $key => $value) {
                $list[$key]['dj_time'] = date('Y-m-d', $value['dj_time']);
                switch ($value['type']) {
                    case 0:
                        $list[$key]['type_name'] = '普通销售';
                        break;
                    case 1:
                        $list[$key]['type_name'] = '销售退货';
                        break;
                    default:
                        # code...
                        break;
                }
            }

            return $this->grid_data($list, $count, $page, $limit);
        }


        $month = $this->get_month_start_to_end();
        $company_name = Db::name('system_company')->where('id', 1)->value('company_name');
        $this->assign('month', $month);
        $this->assign('company_name', $company_name);

        return $this->fetch();
    }

    public function export()
    {
        $data = $this->request->param();
        $rowid = explode(" ", $data['rowid']);
        $title = [
            'dj_time' => '单据日期',
            'sn' => '单据编号',
            'type_name' => '业务类别',
            'saler_user_price' => '销售金额',
            'yh_e' => '优惠金额',
            'user_cd_price' => '客户承担费用',
            'yh_price' => '应收金额',
            'payment' => '实际收款金额',
            'total_arrears' => '应收款余额',
            'mark' => '备注',
        ];

        $arr_list = [];

        $list = Db::name('order')->where('big_type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->select();
        $count = Db::name('order')->where('big_type', 0)->where('dj_time', '>', strtotime($data['startdate']))->where('dj_time', '<', strtotime($data['enddate']))->where('user_id', $data['user_id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->order('id desc')->count();

        foreach ($list as $key => $value) {
            $list[$key]['dj_time'] = date('Y-m-d', $value['dj_time']);
            switch ($value['type']) {
                case 0:
                    $list[$key]['type_name'] = '普通销售';
                    break;
                case 1:
                    $list[$key]['type_name'] = '销售退货';
                    break;
                default:
                    # code...
                    break;
            }
        }

        foreach ($list as $key => $value) {
            $zh = [
                'dj_time' => $value['dj_time'],
                'sn' => $value['sn'],
                'type_name' => $list[$key]['type_name'],
                'saler_user_price' => $value['saler_user_price'],
                'yh_e' => $value['yh_e'],
                'user_cd_price' => $value['user_cd_price'],
                'yh_price' => $value['yh_price'],
                'payment' => $value['payment'],
                'total_arrears' => $value['total_arrears'],
                'mark' => $value['mark'],
            ];
            array_push($arr_list, $zh);
        }

        //添加标题到数组头部
        array_unshift($arr_list, $title);
        $kong_arr = $title;
        foreach ($kong_arr as $ke1 => $va1) {
            $kong_arr[$ke1] = '';
        }
        for ($i = 0; $i < 3; $i++) {
            array_unshift($arr_list, $kong_arr);
        }
        //根据此参数获取数据中的值，要与表格标题键名对应
        $keys = [];
        foreach ($title as $k2 => $v2) {
            array_push($keys, $k2);
        }
        $merge = [];
        $options = [];
        $o_type = -1;
        $merge = [ //合并
            'start' => 5,
            'arr_zimu' => ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
            'keyword' => 'sn',
        ];
        $options = [
            'alignCenter' => ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], //居中
            'bold' => ['A1:J3', 'A4:J4'], //加粗
            'fontSize' => ['zimu' => ['A1'], 'size' => 20], //字体大小
        ];
        $title = [ //标题
            'title' => "客户对账单",
            'max_zimu' => "J",
            'number' => '3',
        ];
        $excel_export_title = "客户对账单_" . date('Y-m-d_His') . '.xlsx'; //文件名称
        $excel = new Excel();
        $excel->exportExcel($arr_list, $keys, $excel_export_title, $options, $o_type, $merge, $title);

        exit();
    }
}
